package com.cn.sl.server.api.system.file.controller

import cn.dev33.satoken.annotation.SaCheckLogin
import cn.dev33.satoken.annotation.SaIgnore
import com.cn.sl.server.api.system.file.model.dto.SysFileDto
import com.cn.sl.server.api.system.file.model.enumerate.FileType
import com.cn.sl.server.api.system.file.service.SysFileService
import com.cn.sl.server.common.constant.Constant
import com.cn.sl.server.common.constant.MessageConstant
import com.cn.sl.server.common.dto.LongIdsDto
import com.cn.sl.server.util.ResponseMessageDto
import com.cn.sl.server.util.ok
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.multipart.MultipartFile
import java.io.IOException

/**
 * 文件上传
 *
 * @author 阮雪锋
 */
@Tag(name = "文件管理")
@SaCheckLogin
@Controller
@RequestMapping("${Constant.ADMIN_ROOT}/sys-file")
class SysFileController(
    val sysFileService: SysFileService,
) {
    val log: Logger = LoggerFactory.getLogger(SysFileController::class.java)

    /**
     * 文件上传
     *
     * @param file 文件对象
     * @param type 文件类型
     * @return ResponseEntity
     */
    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    fun upload(file: MultipartFile, type: FileType): ResponseEntity<SysFileDto> {
        return ok(sysFileService.save(file, type))
    }

    @Operation(summary = "根据id获取文件信息")
    @GetMapping("/find/{id}")
    fun findById(@PathVariable id: Long): ResponseEntity<SysFileDto> {
        return ok(sysFileService.findById(id))
    }

    @Operation(summary = "根据id获取文件信息")
    @PostMapping("/find/ids")
    fun urls(@RequestBody dto: LongIdsDto): ResponseEntity<List<SysFileDto>> {
        return ok(sysFileService.findAllById(dto.ids))
    }

    @Operation(summary = "下载文件")
    @SaIgnore
    @GetMapping("/download/{id}")
    fun downloadById(@PathVariable id: Long) {
        sysFileService.download(id)
    }

    @Operation(summary = "下载所有文件")
    @PostMapping("/downloadAll")
    @Throws(IOException::class)
    fun downloadAll(@RequestBody dto: LongIdsDto) {
        sysFileService.downloadAll(dto.ids)
    }

    /**
     * 下载模板
     */
    @Operation(summary = "下载模板")
    @PostMapping("/download/template/{templateName}")
    fun downloadTemplate(@PathVariable("templateName") templateName: String) {
        sysFileService.downloadTemplate(templateName)
    }

    @Operation(summary = "删除文件")
    @DeleteMapping("/sys/file/delete/{id}")
    fun deleteById(@PathVariable("id") id: Long): ResponseEntity<ResponseMessageDto> {
        sysFileService.remove(id)
        return ok(MessageConstant.DELETE_SUCCESS)
    }
}